package com.teaRestaurant.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.teaRestaurant.common.result.Result;
import com.teaRestaurant.domain.DTO.CategoryPageQueryDTO;
import com.teaRestaurant.domain.entity.Category;
import com.teaRestaurant.service.CategoryService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类相关接口")
@CrossOrigin
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 获取分类分页
     * 查询条件：名称，类别
     */
    @GetMapping
    public Result list ( CategoryPageQueryDTO categoryPageQueryDTO) {
        IPage<Category> categoryPage = categoryService.list(categoryPageQueryDTO);
        return Result.success(categoryPage);

    }

    /**
     * 根据分类类别获取分类列表
     */
    @GetMapping("/getAll")
    public Result allList (@RequestParam Integer type) {
        List<Category> categoryList = categoryService.allList(type);
        return Result.success(categoryList);
    }

    /**
     * 新增分类
     */
    @PostMapping
    public Result insert (@RequestBody Category category){
        System.out.println(category);
        categoryService.insert(category);
        return Result.success();
    }

    /**
     * 删除分类
     */
    @DeleteMapping
    public Result delete (@RequestParam String ids) {
        System.out.println(ids);
        List<Long> ids2 = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .collect(Collectors.toList());
        categoryService.delete(ids2);
        return Result.success();
    }


    /**
     * 修改分类状态
     */
    @PutMapping("/updateStatus/{id}")
    public Result updateStatus (@PathVariable("id") Long id) {
        categoryService.updateStatus(id);
        return Result.success();
    }
}
